package com.webchat.ugc.service.mall;


import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.constants.WebConstant;
import com.webchat.common.enums.LUAScriptConstants;
import com.webchat.common.enums.MessageQueueTaskStatusEnum;
import com.webchat.common.enums.QueueMessageActionEnum;
import com.webchat.common.enums.RedisKeyEnum;
import com.webchat.common.enums.messagequeue.MessageQueueEnum;
import com.webchat.common.enums.messagequeue.MessageQueueMethodEnum;
import com.webchat.common.exception.BusinessException;
import com.webchat.common.service.RedisService;
import com.webchat.common.service.messagequeue.producer.MessageQueueProducer;
import com.webchat.common.service.messagequeue.task.MessageQueueTaskDTO;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.dto.mall.CartShopDTO;
import com.webchat.domain.dto.queue.MallProductStockUpdateMessageDTO;
import com.webchat.domain.dto.queue.MallUserCartMessageDTO;
import com.webchat.domain.vo.dto.mall.MallAddCartSkuDTO;
import com.webchat.domain.vo.response.mall.MallSKUResponseVO;
import com.webchat.domain.vo.response.mall.MallSPUDetailVO;
import com.webchat.domain.vo.response.mall.MallShopResponseVO;
import com.webchat.domain.vo.response.mall.MallUserCartListResponseVO;
import com.webchat.domain.vo.response.mall.MallUserCartSKUResponseVO;
import com.webchat.ugc.messaegqueue.consumer.local.MQTaskWriteDataBaseQueue;
import com.webchat.ugc.repository.dao.mall.IMallUserCartDAO;
import com.webchat.ugc.repository.entity.mall.MallUserCartEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.webchat.common.enums.LUAScriptConstants.CACHE_EXIST;
import static com.webchat.common.enums.LUAScriptConstants.CACHE_EXPIRE_ALL;
import static com.webchat.common.enums.LUAScriptConstants.CACHE_EXPIRE_SHOP;
import static com.webchat.common.enums.LUAScriptConstants.CACHE_EXPIRE_SKU;
import static com.webchat.common.enums.LUAScriptConstants.CACHE_EXPIRE_SKU_TOTAL;
import static com.webchat.common.enums.LUAScriptConstants.CACHE_OK;

@Service
public class MallUserCartService {

    @Autowired
    private IMallUserCartDAO mallUserCartDAO;

    @Autowired
    private MallProductService mallProductService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private MQTaskWriteDataBaseQueue mqTaskWriteDataBaseQueue;

    @Autowired
    private MessageQueueProducer<MallUserCartMessageDTO, Long> messageQueueProducer;


    /**
     * 添加购物车
     * @param userId
     * @param skuId
     * @return
     */
    public boolean add(String userId, Long skuId, Integer skuCount) {
        Date addCartDate = new Date();
        long addCartTime = addCartDate.getTime();
        // 1. 获取sku相关信息（shop信息、商品信息）
        // RPC请求商品服务
        MallSPUDetailVO product = mallProductService.productInfoBySku(skuId);
        Assert.notNull(product, "商品不存在或已下架");
        Long shopId = product.getShopId();
        // 2. 加入购物车缓存
        // 2.1 加入用户收藏sku缓存
        String userCartSkuKey = userCartSkuCacheKey(userId, shopId);
        String userCartSkuNoneCacheKey = userCartSkuNoneCacheKey(userId, shopId);
        String userCartShopKey = userCartShopCacheKey(userId);
        String userCartShopNoneCacheKey = userCartShopNoneCacheKey(userId);
        String userCartSkuCountKey = userCartSkuCountCacheKey(userId);
        String userCartSkuTotalKey = userCartSkuTotalCacheKey();
        // LUA执行保证两个缓存写入原子性
        String res = redisService.executeScript(
                RedisScript.of(LUAScriptConstants.MALL_ADD_CART_SCRIPT, String.class),
                List.of(userCartSkuKey, userCartShopKey, userCartSkuCountKey, userCartSkuNoneCacheKey, userCartShopNoneCacheKey, userCartSkuTotalKey),
                String.valueOf(skuId), String.valueOf(shopId), String.valueOf(skuCount), userId, String.valueOf(addCartTime));
        if (CACHE_EXIST.equals(res)) {
            throw new BusinessException("重复添加");
        } else if (CACHE_OK.equals(res)) {
            // 购物车添加成功
            // 3. 写数据库（MQ）
            MallUserCartMessageDTO message = new MallUserCartMessageDTO(userId, shopId, skuId, skuCount, addCartTime,
                                                                        QueueMessageActionEnum.INSERT.getAction());
            try {
                messageQueueProducer.send(MessageQueueEnum.QUEUE_MALL_USER_CART_UPDATE_MESSAGE, message);
            } catch (Exception e) {
                // TODO 如果分布式队列入队异常最佳实践处理方案参考电商库存扣减实现：
                // com.webchat.pgc.service.mall.MallProductStockService.incrementStock
            }
            // 删除noneCache标记
            delUserCartSkuNoneCacheKey(userId, shopId);
            delUserCartShopNoneCacheKey(userId);
            return true;
        } else if (CACHE_EXPIRE_SKU.equals(res)) {
            doInitUserCartHistorySku(userId, shopId);
            // 重新加入购物车
            add(userId, skuId, skuCount);
        } else if (CACHE_EXPIRE_SHOP.equals(res)) {
            doInitUserCartHistoryShop(userId);
            // 重新加入购物车
            add(userId, skuId, skuCount);
        } else if (CACHE_EXPIRE_SKU_TOTAL.equals(res)) {
            doInitUserCartHistorySkuTotal(userId);
            // 重新加入购物车
            add(userId, skuId, skuCount);
        } else if (CACHE_EXPIRE_ALL.equals(res)) {
            doInitUserCartHistorySku(userId, shopId);
            doInitUserCartHistoryShop(userId);
            doInitUserCartHistorySkuTotal(userId);
            // 重新加入购物车
            add(userId, skuId, skuCount);
        }
        return false;
    }


    public boolean updateCount(String userId, Long skuId, Integer skuCount) {

        /**
         * 更新hash缓存
         */
        String skuCountCacheKey = userCartSkuCountCacheKey(userId);
        if (redisService.exists(skuCountCacheKey)) {
            doInitUerCartSkuCountCache(userId);
        }
        redisService.hset(skuCountCacheKey, String.valueOf(skuId), String.valueOf(skuCount));
        redisService.expire(skuCountCacheKey, RedisKeyEnum.MALL_USER_CART_SKU_COUNT_HASH_CACHE.getExpireTime());

        MallUserCartMessageDTO message = new MallUserCartMessageDTO(userId, skuId,
                QueueMessageActionEnum.UPDATE.getAction());
        message.setSkuCount(skuCount);
        try {
            messageQueueProducer.send(MessageQueueEnum.QUEUE_MALL_USER_CART_UPDATE_MESSAGE, message);
        } catch (Exception e) {
            // TODO 如果分布式队列入队异常最佳实践处理方案参考电商库存扣减实现：
            // com.webchat.pgc.service.mall.MallProductStockService.incrementStock
        }
        return true;
    }

    /**
     * 初始化用户购物车特定店铺下sku缓存列表
     *
     * @param userId
     */
    private void doInitUserCartHistorySku(String userId, Long shopId) {

        String userCartSkuKey = userCartSkuCacheKey(userId, shopId);
        String userCartSkuNoneCacheKey = userCartSkuNoneCacheKey(userId, shopId);
        if (redisService.exists(userCartSkuNoneCacheKey)) {
            // 购物车无数据，不需要查库刷新（防止缓存击穿）
            return;
        }
        List<MallUserCartEntity> carts = mallUserCartDAO.findAllByCreateByAndDeletedAndShopId(
                                         userId, false, shopId);
        if (CollectionUtils.isEmpty(carts)) {
            redisService.set(userCartSkuNoneCacheKey, WebConstant.CACHE_NONE, RedisKeyEnum.MALL_USER_CART_SKU_NONE_CACHE.getExpireTime());
            return;
        }
        Set<ZSetOperations.TypedTuple<String>> tuples = carts.stream().map(cart -> {
            double value = cart.getSkuId();
            double score = cart.getUpdateDate() == null ? cart.getCreateDate().getTime() :
                                                          cart.getUpdateDate().getTime();
            ZSetOperations.TypedTuple<String> typedTuple = new DefaultTypedTuple(value, score);
            return typedTuple;
        }).collect(Collectors.toSet());
        redisService.remove(userCartSkuKey);
        redisService.zadd(userCartSkuKey, tuples, RedisKeyEnum.MALL_USER_CART_SKU_ZSET_CACHE.getExpireTime());
    }


    /**
     * 初始化用户购物车收藏sku总量redis缓存
     *
     * @param userId
     */
    private Long doInitUserCartHistorySkuTotal(String userId) {
        String userCartSkuTotalKey = userCartSkuTotalCacheKey();
        int total = mallUserCartDAO.countByCreateByAndDeleted(userId, false);
        redisService.hset(userCartSkuTotalKey, userId, String.valueOf(total), RedisKeyEnum.MALL_USER_CART_SKU_TOTAL_HASH_CACHE.getExpireTime());
        return (long) total;
    }

    /**
     * 查询用户购物车收藏sku商品总量
     *
     * @param userId
     * @return
     */
    public Long getUserCartSkuTotalCount(String userId) {
        String userCartSkuTotalKey = userCartSkuTotalCacheKey();
        if (redisService.exists(userCartSkuTotalKey)) {
            return this.doInitUserCartHistorySkuTotal(userId);
        }
        String cache = redisService.hget(userCartSkuTotalKey, userId);
        return Long.parseLong(cache);
    }

    /**
     * 初始化用户购物车店铺缓存
     *
     * @param userId
     */
    private void doInitUserCartHistoryShop(String userId) {

        String userCartShopKey = userCartShopCacheKey(userId);
        String userCartShopNoneCacheKey = userCartShopNoneCacheKey(userId);
        if (redisService.exists(userCartShopNoneCacheKey)) {
            // 购物车无店铺数据，不需要查库刷新（防止缓存击穿）
            return;
        }
        List<CartShopDTO> cartShopDTOList = mallUserCartDAO.findAllByUserCartShopId(userId, false);
        if (CollectionUtils.isEmpty(cartShopDTOList)) {
            redisService.set(userCartShopNoneCacheKey, WebConstant.CACHE_NONE, RedisKeyEnum.MALL_USER_CART_SHOP_NONE_CACHE.getExpireTime());
            return;
        }
        Set<ZSetOperations.TypedTuple<String>> tuples = cartShopDTOList.stream().map(cart -> {
            double value = cart.getShopId();
            double score = cart.getCartDate().getTime();
            ZSetOperations.TypedTuple<String> typedTuple = new DefaultTypedTuple(value, score);
            return typedTuple;
        }).collect(Collectors.toSet());
        redisService.remove(userCartShopKey);
        redisService.zadd(userCartShopKey, tuples, RedisKeyEnum.MALL_USER_CART_SKU_ZSET_CACHE.getExpireTime());
    }

    /**
     * 批量初始化缓存购物车商品添加数据
     *
     * @param userId
     */
    private void doInitUerCartSkuCountCache(String userId) {
        String skuCountCacheKey = userCartSkuCountCacheKey(userId);
        long expireTime = RedisKeyEnum.MALL_USER_CART_SKU_COUNT_HASH_CACHE.getExpireTime();
        List<MallUserCartEntity> carts = mallUserCartDAO.findAllByCreateByAndDeleted(userId, false);
        if (CollectionUtils.isEmpty(carts)) {
            redisService.hset(skuCountCacheKey, WebConstant.CACHE_NONE, "0", expireTime);
            return;
        }
        Map<String, String> skuCountMap = carts.stream()
                                          .collect(Collectors.toMap(
                                                  cart -> String.valueOf(cart.getSkuId()),
                                                  cart -> String.valueOf(cart.getSkuCount()),
                                                  (k1, k2) -> k2
                                          ));
        redisService.hmSet(skuCountCacheKey, skuCountMap, expireTime);
    }

    /**
     * 删除购物车特定店铺下无数据标识
     *
     * @param userId
     * @param shopId
     */
    private void delUserCartSkuNoneCacheKey(String userId, Long shopId) {
        String userCartSkuNoneCacheKey = userCartSkuNoneCacheKey(userId, shopId);
        redisService.remove(userCartSkuNoneCacheKey);
    }

    /**
     * 删除购物车无店铺数据标识
     *
     * @param userId
     */
    private void delUserCartShopNoneCacheKey(String userId) {
        String userCartShopNoneCacheKey = userCartShopNoneCacheKey(userId);
        redisService.remove(userCartShopNoneCacheKey);
    }

    /**
     * 用户购物车sku商品列表缓存key
     *
     * @param userId
     * @param shopId
     * @return
     */
    private String userCartSkuCacheKey(String userId, Long shopId) {

        return RedisKeyEnum.MALL_USER_CART_SKU_ZSET_CACHE.getKey(userId, String.valueOf(shopId));
    }

    private String userCartSkuNoneCacheKey(String userId, Long shopId) {

        return RedisKeyEnum.MALL_USER_CART_SKU_NONE_CACHE.getKey(userId, String.valueOf(shopId));
    }

    /**
     * 用户购物车店铺列表缓存key
     *
     * @param userId
     * @return
     */
    private String userCartShopCacheKey(String userId) {

        return RedisKeyEnum.MALL_USER_CART_SHOP_ZSET_CACHE.getKey(userId);
    }

    private String userCartShopNoneCacheKey(String userId) {

        return RedisKeyEnum.MALL_USER_CART_SHOP_NONE_CACHE.getKey(userId);
    }

    /**
     * 用户购物车收藏sku总量
     *
     * @return
     */
    private String userCartSkuTotalCacheKey() {

        return RedisKeyEnum.MALL_USER_CART_SKU_TOTAL_HASH_CACHE.getKey();
    }


    /**
     * 用户购物车sku收藏数量缓存
     *
     * @param userId
     * @return
     */
    private String userCartSkuCountCacheKey(String userId) {

        return RedisKeyEnum.MALL_USER_CART_SKU_COUNT_HASH_CACHE.getKey(userId);
    }

    /**
     * 删除购物车
     *
     *
     * @param userId
     * @param skuId
     * @return
     */
    public boolean remove(String userId, Long skuId) {

        // 1. 删除redis购物车数据
        // 1.1 删除用户购物车收藏sku缓存
        MallSPUDetailVO product = mallProductService.productInfoBySku(skuId);
        Assert.notNull(product, "商品不存在");
        Long shopId = product.getShopId();
        // 2. 加入购物车缓存
        // 2.1 加入用户收藏sku缓存
        String userCartSkuKey = userCartSkuCacheKey(userId, shopId);
        String userCartShopKey = userCartShopCacheKey(userId);
        String userCartSkuCountKey = userCartSkuCountCacheKey(userId);
        String userCartSkuTotalKey = userCartSkuTotalCacheKey();
        String shopIdStr = String.valueOf(shopId);
        // LUA执行保证三个缓存删除原子性
        String res = redisService.executeScript(
                RedisScript.of(LUAScriptConstants.MALL_REMOVE_CART_SCRIPT, String.class),
                List.of(userCartSkuKey, userCartShopKey, userCartSkuCountKey, userCartSkuTotalKey),
                String.valueOf(skuId), shopIdStr, userId);
        if (CACHE_OK.equals(res)) {
            // 删除成功，走队列删除数据库
            MallUserCartMessageDTO message = new MallUserCartMessageDTO(userId, skuId, QueueMessageActionEnum.DELETE.getAction());
            try {
                messageQueueProducer.send(MessageQueueEnum.QUEUE_MALL_USER_CART_UPDATE_MESSAGE, message);
            } catch (Exception e) {
                // TODO 如果分布式队列入队异常最佳实践处理方案参考电商库存扣减实现：
                // com.webchat.pgc.service.mall.MallProductStockService.incrementStock
            }
        }
        return true;
    }

    /**
     * 购物车列表
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public APIPageResponseBean<MallUserCartListResponseVO> page(String userId, Integer pageNo, Integer pageSize) {

        // 1. 走redis翻页获取购物车店铺里列表
        String userCartShopCacheKey = userCartShopCacheKey(userId);
        if (!redisService.exists(userCartShopCacheKey)) {
            // 初始化用户购物车店铺缓存（这里需要注意防击穿）
            doInitUserCartHistoryShop(userId);
        }
        int start = (pageNo - 1) * pageSize;
        int end = start + pageSize;
        long total = redisService.zsize(userCartShopCacheKey);
        if (total == 0) {
            // 总量是0
            return APIPageResponseBean.success(pageNo, pageSize, total, Collections.emptyList());
        }
        Set<String> shopIdSet = redisService.zreverseRange(userCartShopCacheKey, start, end);
        List<Long> shopIdList = shopIdSet.stream().map(Long::parseLong).toList();
        // 2. 批量取店铺下收藏sku列表 skuid
        List<String> userCartShopSkuCacheKeys = shopIdSet.stream().map(shopId ->
                                                userCartSkuCacheKey(userId, Long.valueOf(shopId))).toList();

        /**
         * 初始化购物车店铺下sku缓存（只刷新失效的）
         */
        for (int i = 0; i < shopIdList.size(); i++) {
            if (!redisService.exists(userCartShopSkuCacheKeys.get(i))) {
                doInitUserCartHistorySku(userId, shopIdList.get(i));
            }
        }

        Map<String, Set<String>> shopSkus = redisService.zreverseRangeByPipelined(
                                    userCartShopSkuCacheKeys, new ArrayList<>(shopIdSet), 0, Long.MAX_VALUE);
        if (CollectionUtils.isEmpty(shopSkus)) {
            return APIPageResponseBean.success(pageNo, pageSize, total, Collections.emptyList());
        }
        List<Long> pageCartSkuIds = new ArrayList<>();
        Map<Long, List<Long>> userCartShopSkuMap = new HashMap<>();
        for (Map.Entry<String, Set<String>> entry : shopSkus.entrySet()) {
            Long shopId = Long.parseLong(entry.getKey());
            List<Long> shopCartSkuIds = entry.getValue().stream().map(tt -> Long.parseLong(tt)).toList();
            userCartShopSkuMap.put(shopId, shopCartSkuIds);
            pageCartSkuIds.addAll(shopCartSkuIds);
        }
        // 3. hmget 批量获取每个人sku用户收藏量
        // 判断购物车商品收藏数量缓存是否失失效
        String userCartSkuCountCacheKey = userCartSkuCountCacheKey(userId);
        if (!redisService.exists(userCartSkuCountCacheKey)) {
            // TODO 初始化
            doInitUerCartSkuCountCache(userId);
        }
        // 创建 <skuId:skuCount> map容器
        Map<Long, Integer> skuCountMap = new HashMap<>();
        List<String> skuIdStrList = pageCartSkuIds.stream().map(String::valueOf).toList();
        List<String> skuCountList = redisService.hmget(userCartSkuCountCacheKey, skuIdStrList);
        for (int i = 0; i < pageCartSkuIds.size(); i++) {
            Long skuId = pageCartSkuIds.get(i);
            String count = skuCountList.get(i);
            skuCountMap.put(skuId, StringUtils.isBlank(count) ? 0 : Integer.parseInt(count));
        }
        // 4. RPC请求商品服务获取sku对应商品、店铺等信息
        Map<Long, MallSPUDetailVO> productDetailMap = mallProductService.batchGetProductMap(pageCartSkuIds);
        if (CollectionUtils.isEmpty(productDetailMap)) {
            return APIPageResponseBean.success(pageNo, pageSize, total, Collections.emptyList());
        }
        // 从商品列表中提取所有店铺信息
        Map<Long, MallShopResponseVO> shopDetailMap = productDetailMap.values().stream().collect(Collectors.toMap(MallSPUDetailVO::getShopId, p -> p.getShop(), (p1, p2) -> p2));
        List<MallUserCartListResponseVO> userCartList = new ArrayList<>();
        for (Long shopId : shopIdList) {
            // 获取店铺下用户购物车添加的所有skuId
            List<Long> skuIdList = userCartShopSkuMap.get(shopId);
            if (CollectionUtils.isEmpty(skuIdList)) {
                continue;
            }
            MallShopResponseVO shopDetail = shopDetailMap.get(shopId);
            if (shopDetail == null) {
                continue;
            }
            // 获取当前店铺下所有购物车添加商品详情
            List<MallUserCartSKUResponseVO> cartSkuList =
                    skuIdList.stream().map(skuId -> {
                          MallSPUDetailVO product = productDetailMap.get(skuId);
                          MallUserCartSKUResponseVO cartSku = new MallUserCartSKUResponseVO();
                          cartSku.setSkuCount(skuCountMap.getOrDefault(skuId, 1));
                          // 取当前sku对象
                          MallSKUResponseVO skuInfo = product.getSkuList().stream().filter(sku ->
                                sku.getId().equals(skuId)).toList().get(0);
//                          cartSku.setProductImage(skuInfo.getImage());
                          cartSku.setProductImage(product.getImages().get(0));
                          cartSku.setProductId(product.getId());
                          cartSku.setProductTitle(product.getMainTitle());
                          cartSku.setSku(skuInfo);
                          return cartSku;
                      }).toList();
            MallUserCartListResponseVO userCart = new MallUserCartListResponseVO();
            userCart.setShop(shopDetail);
            userCart.setSkuList(cartSkuList);
            userCartList.add(userCart);
        }
        APIPageResponseBean apiPageResponseBean = APIPageResponseBean.success(pageNo, pageSize, total, userCartList);

        // 获取用户购物车收藏sku总量
        apiPageResponseBean.setExtTotal(getUserCartSkuTotalCount(userId));
        return apiPageResponseBean;
    }

    @Transactional(rollbackFor = Exception.class)
    public void doConsumeQueueMessage(MallUserCartMessageDTO message) {
        if (QueueMessageActionEnum.INSERT.getAction() == message.getAction()) {
            doSave(message);
        } else if (QueueMessageActionEnum.DELETE.getAction() == message.getAction()) {
            doDelete(message);
        } else if (QueueMessageActionEnum.UPDATE.getAction() == message.getAction()) {
            doUpdate(message);
        }
    }

    public void doDelete(MallUserCartMessageDTO message) {

        mallUserCartDAO.deleteAllByCreateByAndSkuId(message.getUserId(), message.getSkuId());
    }

    public void doUpdate(MallUserCartMessageDTO message) {

        mallUserCartDAO.updateUserCartSkuCount(message.getUserId(), message.getSkuId(), message.getSkuCount());
    }

    public void doSave(MallUserCartMessageDTO message) {
        Date cartDate = new Date(message.getCartTime());
        MallUserCartEntity mallUserCartEntity = mallUserCartDAO.findAllByCreateByAndSkuId(message.getUserId(), message.getSkuId());
        if (mallUserCartEntity == null) {
            mallUserCartEntity = new MallUserCartEntity();
            mallUserCartEntity.setCreateBy(message.getUserId());
            mallUserCartEntity.setSkuId(message.getSkuId());
            mallUserCartEntity.setShopId(message.getShopId());
            mallUserCartEntity.setCreateDate(cartDate);
        }
        mallUserCartEntity.setUpdateBy(message.getUserId());
        mallUserCartEntity.setUpdateDate(cartDate);
        mallUserCartEntity.setDeleted(false);
        mallUserCartEntity.setSkuCount(message.getSkuCount());
        mallUserCartDAO.save(mallUserCartEntity);
    }

    /**
     * 分布式队列消费异常且归队异常，记录失败任务
     *
     * @param message
     */
    public void saveFailQueueTask(MallUserCartMessageDTO message) {
        MessageQueueTaskDTO taskDTO = buildMessageQueueTaskDTO(message);
        // 放入本地队列
        try {
            mqTaskWriteDataBaseQueue.push(taskDTO);
        } catch (Exception pushException) {
            // TODO
        }
    }

    private MessageQueueTaskDTO buildMessageQueueTaskDTO(MallUserCartMessageDTO message) {
        MessageQueueTaskDTO task = new MessageQueueTaskDTO();
        task.setTaskId(message.getTaskId());
        task.setTaskJson(JsonUtil.toJsonString(message));
        task.setStatus(MessageQueueTaskStatusEnum.ERROR.getStatus());
        task.setMethod(MessageQueueMethodEnum.SEND.getMethod());
        task.setCreateDate(new Date());
        task.setTopicName(MessageQueueEnum.QUEUE_MALL_USER_CART_UPDATE_MESSAGE.getQueue());
        return task;
    }
}
